<!DOCTYPE html>
<html style="height: 100%">
  <head>
    <meta charset="utf-8" />
    <input id="jsonName" value="hongqiao.json" />
    <p style="font-size: 20px;color: red;">
      输入地址的json文件名导出带有WGS84坐标的csv文件
    </p>
    <button onclick="tableToExcel()">导出</button>
  </head>

  <body style="height: 100%; margin: 0">
    <div id="container" style="height: 100%"></div>
    <script
      type="text/javascript"
      src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"
    ></script>
    <script
      type="text/javascript"
      src="https://webapi.amap.com/maps?v=1.4.12&key=ee159978ca3b99797463996f84cdc592&plugin=AMap.Geocoder"
    ></script>
    <script type="text/javascript" src="gps.js"></script>
    <script type="text/javascript">
      function tableToExcel() {
        $.get('json/' + $('#jsonName').val(), function(data) {
          var addresses = []
          var geocoder = new AMap.Geocoder({
            city: '021' //城市设为北京，默认：“全国”
          })
          var coordsData = [].concat.apply(
            [],
            data.map(function(obj, idx) {
              addresses.push(obj.business_address)
            })
          )
          var addresseGroups = []
          for (var i = 0; i < addresses.length; i += 10) {
            addresseGroups.push(addresses.slice(i, i + 10))
          }
          var gaodeCoords = []
          var gaodeCoordsContact = []
          var num = 0 //计数器 全部返回才可以导出
          addresseGroups.map(function(obj, idx) {
            geocoder.getLocation(obj, function(status, result) {
              if (status === 'complete' && result.geocodes.length) {
                num = num + 1
                gaodeCoords[idx] = result.geocodes
                console.log(idx)
                for (var i = 0; i < result.geocodes.length; i++) {
                  var gaodeCoordsObj = {}
                  gaodeCoordsObj.id = idx * 10 + i + 1
                  if (result.geocodes[i]) {
                    gaodeCoordsObj.formattedAddress = result.geocodes[i]
                      .formattedAddress
                      ? result.geocodes[i].formattedAddress
                      : '未匹配'
                    gaodeCoordsObj.gaodeLat = result.geocodes[i].location.lat
                      ? result.geocodes[i].location.lat
                      : '未匹配'
                    gaodeCoordsObj.gaodeLng = result.geocodes[i].location.lng
                    gaodeCoordsObj.wgsLat = GPS.gcj_decrypt_exact(
                      result.geocodes[i].location.lat,
                      result.geocodes[i].location.lng
                    ).lat
                    gaodeCoordsObj.wgsLng = GPS.gcj_decrypt_exact(
                      result.geocodes[i].location.lat,
                      result.geocodes[i].location.lng
                    ).lon
                    gaodeCoordsObj.level = result.geocodes[i].level
                    gaodeCoordsContact[idx * 10 + i] = gaodeCoordsObj
                  }
                }
                // if (num == (addresseGroups.length-1)) {

                if (num == addresseGroups.length ) {
                  //console.log(gaodeCoordsContact)
                  var jsonData = gaodeCoordsContact
                  let str = `id,formattedAddress,lat,lng,wgsLat,wgsLng,level\n`
                  //增加\t为了不让表格显示科学计数法或者其他格式
                  for (let i = 0; i < jsonData.length; i++) {
                    for (let item in jsonData[i]) {
                      // str += `${jsonData[i][item] + '\t'},`;
                      str += `${jsonData[i][item]},`
                    }
                    str += '\n'
                  }
                  //encodeURIComponent解决中文乱码
                  let uri =
                    'data:text/csv;charset=utf-8,\ufeff' +
                    encodeURIComponent(str)
                  //通过创建a标签实现
                  var link = document.createElement('a')
                  link.href = uri
                  //对下载的文件命名
                  link.download =
                    $('#jsonName')
                      .val()
                      .split('.')[0] + '.csv'
                  document.body.appendChild(link)
                  link.click()
                  document.body.removeChild(link)
                }
              }
            })
          })
        })
      }
    </script>
  </body>
</html>
